package com.gryffonx.utils;

import com.gryffonx.Interfaces.generators.NameGenerators;

import javax.swing.*;
import java.io.*;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.ResourceBundle;

/**
 * @author Erik Stell
 * @version 1.0
 */
public class StringUtils {
    public StringUtils() {
    }

    /**
     * @param str
     * @return
     */
    public static String capitalize(String str) {
        int strLen;

        if ((str == null) || ((strLen = str.length()) == 0)) {
            return str;
        }

        return new StringBuffer(strLen).append(Character.toTitleCase(str.charAt(0))).append(str.substring(1)).toString();
    }

    /**
     * @param str
     * @return
     */
    public static String capitalise(String str) {
        return capitalize(str);
    }

    /**
     * @param str
     * @return
     */
    private static String uncapitalize(String str) {
        int strLen;

        if ((str == null) || ((strLen = str.length()) == 0)) {
            return str;
        }

        return new StringBuffer(strLen).append(Character.toLowerCase(str.charAt(0))).append(str.substring(1)).toString();
    }

    /**
     * @param str
     * @return
     */
    public static String uncapitalise(String str) {
        return uncapitalize(str);
    }

    /**
     * @param str
     * @return
     */
    public static String swapCase(String str) {
        int strLen;

        if ((str == null) || ((strLen = str.length()) == 0)) {
            return str;
        }

        StringBuffer buffer = new StringBuffer(strLen);
        char ch;

        for (int i = 0; i < strLen; i++) {
            ch = str.charAt(i);

            if (Character.isUpperCase(ch)) {
                ch = Character.toLowerCase(ch);
            } else if (Character.isTitleCase(ch)) {
                ch = Character.toLowerCase(ch);
            } else if (Character.isLowerCase(ch)) {
                ch = Character.toUpperCase(ch);
            }

            buffer.append(ch);
        }

        return buffer.toString();
    }

    /**
     * @param fileName
     * @return
     */
    public static Hashtable parseProperties(String fileName) {
        Hashtable retVal = new Hashtable();

        try {
            ResourceBundle map = ResourceBundle.getBundle(fileName);

            for (Enumeration e = map.getKeys(); e.hasMoreElements();) {
                String key = (String) e.nextElement();
                String value = map.getString(key);
                retVal.put(key, value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return retVal;
    }

    /**
     * Use a PrintWriter wrapped around a BufferedWriter,
     * which in turn is wrapped around a FileWriter, to
     * write the string data to the given file.
     *
     * @param file
     * @param dataString
     * @return
     */
    public static boolean writeFile(File file,
                                    String dataString) {

        try {
            PrintWriter out =
                    new PrintWriter(new BufferedWriter(new FileWriter(file)));
            out.print(dataString);
            out.flush();
            out.close();
        } catch (IOException e) {
            return false;
        }
        return true;
    }

    /**
     * Use a BufferedReader wrapped around a FileReader
     * to read the text data from the given file.
     *
     * @param file
     * @return
     */
    public static String readFile(File file) {

        StringBuffer fileBuffer;
        String fileString;
        String line;

        try {

            FileReader in = new FileReader(file);
            BufferedReader dis =
                    new BufferedReader(in);
            fileBuffer = new StringBuffer();

            while ((line = dis.readLine()) != null) {
                fileBuffer.append(line).append("\n");
            }

            in.close();
            fileString = fileBuffer.toString();
        } catch (IOException e) {
            return null;
        }

        return fileString;
    }

    /**
     * Returns an ImageIcon, or null if the path was invalid.
     *
     * @param path
     * @return
     */
    public static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = NameGenerators.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
}